package com.mashibing.serviceverificationcode.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.mashibing.internalcommon.dto.ResponseResult;
import com.mashibing.internalcommon.response.NumberCodeResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

@RestController
public class ServiceVerificationCodeController {

//    @Value("${spring.cloud.nacos.discovery.cluster-name}")
//    String clusterName;


    @GetMapping("/numberCode/{size}")
    @SentinelResource("numberCode")
    public ResponseResult numberCode(@PathVariable int size, HttpServletRequest request) {

        String tag = request.getHeader("tag");
        System.out.println("请求头tag=" + tag);

        // 生成验证码
        int random = (int) ((Math.random() * 9 + 1) * Math.pow(10, size - 1));
        System.out.println("验证码为" + random);
        NumberCodeResponse numberCodeResponse = new NumberCodeResponse();
        numberCodeResponse.setNumberCode(random + "");
//        System.out.println("访问机器为：" + clusterName);

//        try {
//            TimeUnit.MINUTES.sleep(1);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }

        // 返回
        return ResponseResult.success(numberCodeResponse);
    }
}
